﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SAD_SiS.Models
{
    public class Cart
    {
        public List<CartLine> lineCollection = new List<CartLine>();

        public void AddBook(GeneralProduct b, int quantity)
        {
            int matchID = -1;

            if (lineCollection.Count == 0)
            {
                lineCollection.Add(new CartLine(b, quantity));
            }
            else
            {
                for (int i = 0; i < lineCollection.Count; i++)
                {
                    CartLine line = lineCollection[i];
                    if (line.Product.P.ProductID == b.P.ProductID && line.Product.Details.Count == b.Details.Count)
                    {
                        matchID = i;
                        for (int j = 0; j < line.Product.Details.Count; j++)
                        {
                            if (line.Product.Details[j].id != b.Details[j].id)
                            {
                                matchID = -1;
                                break;
                            }

                        }
                    }
                    if (matchID != -1)
                    {
                        break;
                    }
                }
                if (matchID == -1)
                {
                    lineCollection.Add(new CartLine(b, quantity));
                }
                else
                {
                    lineCollection[matchID].Quantity += quantity;
                }
            }

        }

        public void UpdateQuantity(int ProductID, int IndexID, int quantity)
        {
            if (IndexID != 0)
            {
                lineCollection[IndexID].Quantity = quantity;
                if (lineCollection[IndexID].Quantity == 0)
                {
                    lineCollection.Remove(lineCollection[IndexID]);
                }
            }
            else
            {
                CartLine line = lineCollection.Where(p => p.Product.P.ProductID == ProductID).FirstOrDefault();
                if (line == null)
                {
                }
                else
                {
                    line.Quantity = quantity;
                }
                if (line.Quantity == 0)
                {
                    //lineCollection.Remove(line);
                }
            }
        }

        public void Clear()
        {
            lineCollection.Clear();
        }

        public bool IsEmpty()
        {
            if (lineCollection.Count == 0)
            {
                return true;
            }
            else
            {
                for (int i = 0; i < lineCollection.Count; i++)
                {
                    if (lineCollection[i].Quantity != 0)
                    {
                        return false;
                    }
                }
                return true;
            }
            
        }

        public double GetTotal()
        {
            double sum = 0;
            for (int i = 0; i < lineCollection.Count; i++)
            {
                sum += lineCollection[i].Quantity * (double)lineCollection[i].Product.P.SellPrice;
            }
            return sum;
        }

        public class CartLine
        {
            public GeneralProduct Product { get; set; }
            public int Quantity { get; set; }

            public CartLine()
            {

            }
            public CartLine(GeneralProduct b, int quantity)
            {
                this.Product = b;
                this.Quantity = quantity;
            }
        }

    }

    public class GeneralProduct
    {
        public V_Product P { get; set; }
        public List<PairValue> Details { get; set; }

    }

    public class PairValue
    {
        public int id { get; set; }
        public string name { get; set; }
        public string value { get; set; }
    }
}